<?php

namespace app\models\Child;

use Yii;

/**
 * This is the model class for table "user_code".
 *
 * @property integer $id
 * @property string $user_phone
 * @property integer $rand_num
 * @property integer $create_time
 */
class UserCode extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user_code';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['rand_num', 'create_time'], 'integer'],
            [['user_phone'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_phone' => 'User Phone',
            'rand_num' => 'Rand Num',
            'create_time' => 'Create Time',
        ];
    }
    
   /*
     * 存储验证码
     */
    public function setCode($user_phone,$rand_num,$create_time)
    {
        $list = $this->find()
                ->where(['user_phone' => $user_phone])
                ->one();
        if(empty($list['id']))
        {
            $this->user_phone = $user_phone;
            $this->rand_num = $rand_num;
            $this->create_time = $create_time;
            return $this->save();
        }else{
            $list->user_phone = $user_phone;
            $list->rand_num = $rand_num;
            $list->create_time = $create_time;
            return $list->save();
        }
    }
    
    /*
     * 通过手机号获取验证码
     */
    public function getList($user_phone)
    {
        $list = $this->find()
                ->where(['user_phone' => $user_phone])
                ->asArray()
                ->one();
        return $list;
    }
}
